package com.z.sys.controller;


import com.z.base.PageTableRequest;
import com.z.base.ResponseCode;
import com.z.base.Results;
import com.z.sys.model.User;
import com.z.sys.service.IUserService;
import com.z.sys.vo.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zxy
 * @since 2020-10-13
 */
@Controller
@RequestMapping("/sys/user")
@Api(tags = "用户相关")
public class UserController {

    @Autowired
    private IUserService userService;


    @GetMapping("list")
    @ApiOperation("用户列表分页查询")
    @ResponseBody
    public Results list(PageTableRequest page) {
        page.countOffset();
        Results<User> results = userService.findByPage(page.getOffset(), page.getLimit());
        return results;
    }

    @GetMapping("listByMap")
    @ApiOperation("用户列表分页条件查询")
    @ResponseBody
    public Results listByMap(PageTableRequest page, HttpServletRequest request) {
        page.countOffset();

        Map<String, Object> map = new HashMap<>();
        String username = request.getParameter("sys/username");
        if(!StringUtils.isEmpty(username))
            map.put("sys/username_like_s", username);
        String id = request.getParameter("id");
        if(!StringUtils.isEmpty(id))
            map.put("id_eq_s", id);
        String phone = request.getParameter("phone");
        if(!StringUtils.isEmpty(phone))
            map.put("phone_eq_s", phone);
        Results<User> results = userService.findPageByMap(map, page.getOffset(), page.getLimit());
        return results;
    }

    @GetMapping("toList")
    @ApiOperation("用户列表页面")
    public String toList() {
        return "sys/user/userList";
    }

    @ApiOperation("用户添加页面")
    @GetMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("sysUser", new User());
        return "sys/user/userAdd";
    }

    @ApiOperation("用户修改页面")
    @GetMapping("toEdit")
    public String toEdit(Model model, String id) {
        if(StringUtils.isEmpty(id)){
            return "参数异常";
        }
        model.addAttribute("sysUser", userService.findById(id));
        return "sys/user/userEdit";
    }

    @ApiOperation("用户添加接口")
    @PostMapping("add")
    @ResponseBody
    public Results add(UserDto user, Integer roleId){
        try{
            List<User> userDBs = userService.findUserByPhone(user.getPhone());
            if(userDBs != null && userDBs.size()>0 ){
                throw new Exception("手机号码已存在");
            }
            user.setStatus(1);
            user.setPassword(new BCryptPasswordEncoder().encode("12323"));
            user.setCreateTime(new Date());
            userService.save(user, roleId);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

    @ApiOperation("用户修改接口")
    @PostMapping("update")
    @ResponseBody
    public Results update(UserDto user, Integer roleId){
        try{
            List<User> userDBs = userService.findUserByPhone(user.getPhone());
            if(userDBs != null && userDBs.size()>0 ){
                for(User userDB : userDBs){
                    if(!(user.getId().equals(userDB.getId()))){
                        throw new Exception("手机号已被占用！");
                    }
                }
            }
            user.setUpdateTime(new Date());
            userService.update(user, roleId);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

    @ApiOperation("用户删除接口--单个")
    @PostMapping("del")
    @ResponseBody
    public Results del(String id){
        try{
            userService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

    @ApiOperation("用户删除接口--多个")
    @PostMapping("delAll")
    @ResponseBody
    public Results delAll(String ids){
        try{
            for(String id : ids.split(","))
                userService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

}